package com.highveldsky.gwtdatabinding.shared.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.persistence.Id;
import javax.persistence.Transient;

import com.google.gwt.user.client.rpc.IsSerializable;

public class Car implements IsSerializable{

	@Id private Long id;
	private String make;
	private String model;
	private Integer price;
	private Integer mileage;
	private Integer year;
	@Transient private transient PropertyChangeSupport changes = new PropertyChangeSupport(this);
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		Long old = this.id;
		this.id = id;
		changes.firePropertyChange("id", old, id);
	}
	public String getMake() {
		return make;
	}
	public void setMake(String make) {
		String old = this.make;
		this.make = make;
		changes.firePropertyChange("make", old, make);
	}
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		String old = this.model;
		this.model = model;
		changes.firePropertyChange("model", old, model);
	}
	public Integer getPrice() {
		return price;
	}
	public void setPrice(Integer price) {
		Integer old = this.price;
		this.price = price;
		changes.firePropertyChange("price", old, price);
	}
	public Integer getMileage() {
		return mileage;
	}
	public void setMileage(Integer mileage) {
		Integer old = this.mileage;
		this.mileage = mileage;
		changes.firePropertyChange("mileage", old, mileage);
	}
	public Integer getYear() {
		return year;
	}
	public void setYear(Integer year) {
		Integer old = this.year;
		this.year = year;
		changes.firePropertyChange("year", old, year);
	}
	public void set(String propertyName, String value) {
		if (propertyName.equals("id")) {
			this.setId(Long.parseLong(value));
		} else if (propertyName.equals("make")) {
			this.setMake(value);
		} else if (propertyName.equals("model")) {
			this.setModel(value);
		} else if (propertyName.equals("year")) {
			this.setYear(Integer.parseInt(value));
		} else if (propertyName.equals("mileage")) {
			this.setMileage(Integer.parseInt(value));			
		} else if (propertyName.equals("price")) {
			this.setPrice(Integer.parseInt(value));
		}
	}
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		changes.addPropertyChangeListener(listener);
	}
	public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
		changes.addPropertyChangeListener(propertyName, listener);
	}
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		changes.removePropertyChangeListener(listener);
	}
	public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
		changes.removePropertyChangeListener(propertyName, listener);
	}
	public String toString() {
		return "Car [id=" + id + ", make=" + make + ", model=" + model
				+ ", price=" + price + ", mileage=" + mileage + ", year="
				+ year + "]";
	}
	
}
